<?php
/*
 * 系统异常，仅需要中止PHP的调用
 */
function SysDie($type, $code, $param=null)
{
	global $err_type;
	global $err_code;
	$now = date('Y-m-d H:i:s');
	$msg = $now . "\t" . $err_type[$type] . "\t" .  $err_code[$code];
	if($param != null)
		$msg .=  "\t" . $param;
	global $conf;
	error_log($msg . "\n", 3, $conf['dielog']);
	debug_log($msg);
	$msg = $now . "\t" . "系统异常,有疑问联系客服";
	$msg .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	die($msg);
}

/*
 * 调试日志
 */
function debug_log($log)
{
	if(!DEBUG)
		return;
	$html = <<<EOF
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<div style="border: 3px solid #ff00ff; overflow-x:auto">
<h1>方框里的内容是调试日志，正式服不会显示</h1>
<pre>
$log;
</pre>
</div>
EOF;
	echo $html;
}

/*
 * 请求非法
 */
function request_error($errcode, $errmsg)
{
	$log = "request error\nerrcode: " . $errcode . "\nerrmsg: " . $errmsg;
	$log .= "\nGET:" . var_export($_GET, 1);
	$log .= "\nPOST:" . var_export($_POST, 1);
	$log .= "\nFILES:" . var_export($_FILES, 1);
	sys_log($log);
	debug_log($log);
	$msg = "请求异常,有疑问联系客服";
	global $ajax;
	if(!$ajax)
		$msg .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	die($msg);
}

/*
 * 完整打印客户端参数，遭遇非法访问时调用
 */
function full_log()
{
	$log = "\nrequest:\n" . var_export($_REQUEST, 1) . "\nserver:\n" . var_export($_SERVER, 1);
	sys_log($log);
	debug_log($log);
	$msg = "参数异常,有疑问联系客服";
	global $ajax;
	if(!$ajax)
		$msg .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	die($msg);
}

/*
 * 系统日志输出，单纯的打印日志
 */
function sys_log($log)
{
	global $conf;
	$outlog = date('Y-m-d H:i:s') . "\t" . $_SERVER["REMOTE_ADDR"];
	if(isset($_SERVER['HTTP_USER_AGENT']))
		$outlog .= "\t" . $_SERVER['HTTP_USER_AGENT'];
	$outlog .= "\t" . $log . "\n";
	error_log($outlog, 3, $conf['logfile']);
}

